<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=1000, initial-scale=1.0, maximum-scale=1.0">
	    <!-- Loading Bootstrap -->
	    <link href="Public/Admin/Flat/dist/css/vendor/bootstrap.min.css" rel="stylesheet">
	    <!-- Loading Flat UI -->
	    <link href="Public/Admin/Flat/dist/css/flat-ui.css" rel="stylesheet">
	    <link href="Public/Admin/Flat/docs/assets/css/demo.css" rel="stylesheet">
	    <link rel="shortcut icon" href="Public/Admin/Flat/img/favicon.ico">
	<script src="resource/hdjs/js/jquery.min.js"></script>
	    <script src="resource/hdjs/js/bootstrap.min.js"></script>	
	    <!-- HTML5 shim, for IE6-8 support of HTML5 elements. All other JS at the end of file. -->
	    <!--[if lt IE 9]>
	      <script src="dist/js/vendor/html5shiv.js"></script>
	      <script src="dist/js/vendor/respond.min.js"></script>
	    <![endif]-->
	</head>
	<body>
		<table class="table table-hover">
			<tr class="active">
				<td width="80">货品id</td>
			<?php foreach ((array)$cmData as $k=>$v){?>
				<td width=""><?php echo $v['taname']?></td>
				<?php }?>
				<td width="40">库存</td>
				<td width="">货号</td>
				<td width="150">操作</td>
			</tr>
			<?php foreach ((array)$data as $k=>$v){?>
			<tr>
				<td><?php echo $v['glid']?></td>
				<?php foreach ((array)$v['combine'] as $kk=>$vv){?>
				<td><?php echo $vv?></td>
				<?php }?>
				
				<td><?php echo $v['inventory']?></td>
				<td><?php echo $v['number']?></td>
				<td>
					<a name="<?php echo $v['glid']?>" href="javascript:;" data-target=".bs-example-modal-sm" data-toggle="modal" class="btn btn-sm btn-warning edit">编辑</a>
					<a href="<?php echo U('del',array('gid'=>$gid,'glid'=>$v['glid']))?>" class="btn btn-sm btn-danger">删除</a>
				</td>
			</tr>
			<?php }?>
			<!--添加货品-->
			
			<form action="<?php echo U('admin/goodslist/add')?>" method="post">
				
			<tr>
				<td>添加货品</td>
				<?php foreach ((array)$cmData as $k=>$v){?>
				<td width="40">
					<select name="combine[]">
						<option value="0">请选择</option>
						<?php foreach ((array)$v['old'] as $kk=>$vv){?>		
						<option value="<?php echo $vv['gtid']?>"><?php echo $vv['gtvalue']?></option>
						<?php }?>	
					</select>
				</td>
				<?php }?>
				<!--库存-->
				<td width="">
					<input type="text" name="inventory" id="" value="" />
				</td>
				<!--货号-->
				<td width="40">
					<input type="text" name="number" id="" value="" />
				</td>
				<td>
					<input type="hidden" name="gid" id="gid" value="<?php echo $gid?>" />
					<input class="btn btn-sm btn-warning" type="submit" value="提 交"/>
				</td>
			</tr>
							
			</form>
		</table>

<!-- Small modal -->

<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
	<form action="" method="post" id="form" onsubmit="return add();">
  <div class="modal-dialog modal-sm">
    <div class="modal-content" style="padding: 30px;">
    
      
    </div>
  </div>
	</form>
  
</div>

<script type="text/javascript">
	$(function(){
		$('.edit').click(function(){
			This=$(this).parents('tr').html();
//			alert(This);
			var glid=$(this).attr('name');
//			alert($glid);
			$.ajax({
				type:"post",
				url:"<?php echo U('edData')?>",
				data:{glid:glid},
				success:function(phpData){
					var str='';
					if(!phpData.valid){
						return;
					}
//					alert(phpData);
					str+='<table class="table table-hover" border="0" cellspacing="0" cellpadding="0"><tr>货品ID:'+phpData.message.glid+'</tr>';
					$.each(phpData.message.comname, function(k,v) {
						$.each(v, function(kk,vv) {
							str+='<tr><td>'+kk+'：</td><td><input readonly="readonly" value="'+vv+'"/></td></tr>';
						});
						
					});
					str+='<tr><td>库存：</td><td><input type="text" name="inventory" id="" value="'+phpData.message.inventory+'" /></td></tr>';
					str+='<tr><td>货号：</td><td><input type="text" name="number" id="" value="'+phpData.message.number+'" /></td></tr><input type="hidden" name="gid" id="gid" value="'+phpData.message.gid+'" /><input type="hidden" name="glid" id="gid" value="'+phpData.message.glid+'" /></table>';
					str+='<input style="margin-left: 180px;" class="btn btn-sm btn-warning" type="submit" value="提 交"/>';
//    			alert(str);
      			$('.modal-content').html(str);
				}
			});
		})
		
	})
</script>
	<script type="text/javascript">
		function add(){
//			序列化表单数据serialize()
			var $gid=$('input[name=gid]').val();
			var data=$('#form').serialize();
			$.post("<?php echo U('edit',array('gid'=>$gid))?>",data,function(phpData){
				require(['util'],function(util){
					if(phpData.valid){
						util.message(phpData.message,"<?php echo U('index')?>",'success');
					}else{
						util.message(phpData.message,'','error');
					}
				});
			},'json');
		}
	</script>
		
	<script type="text/javascript">
	var sel = $('select[name="combine[]"]');
	var len = sel.length;
	sel.change(function () {
//		alert(len);
		var data={
			'gid':<?php echo $gid?>,
			'combine':{}
		}
		for(var i=0;i<len;++i){
			if(sel.eq(i).val()==0){
				return;
			}else{
				data.combine[i]=sel.eq(i).val();
			}
		}
		$.ajax({
			url:"<?php echo U('goodslist/dete')?>",
			type:'post',
			data:data,
			dataType:'json',
			success:function(phpData){
				if(phpData.message==0){
					alert('货品已存在，修改库存请点击修改');
					for(var i=0;i<len;++i){
						sel.eq(i).val(0);
					}
				}
			}
		})
		
		
		
	});
</script>
	
	
	
	
	
	
		
		
	</body>
</html>
